package com.prs.crm.dao.ca.work.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.ca.work.TookParticularDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.ca.work.TookParticular;

public class TookParticularDaoImpl extends BaseDaoImpl<TookParticular>
		implements TookParticularDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<TookParticular> getAllTookParticular() {
		return (List<TookParticular>) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session
						.createCriteria(TookParticular.class);
				return criteria.list();
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public TookParticular getTookParticularByNameId(final int tpArticlenameId) {
		return (TookParticular) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session
						.createCriteria(TookParticular.class);
				criteria.add(Restrictions
						.eq("tpArticlenameId", tpArticlenameId));
				List<TookParticular> took = criteria.list();
				if (took.size() == 0) {
					return null;
				} else {
					return took.get(0);
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public TookParticular getTookParticularByTookApplyPerso(
			final String atTookApplyPerson) {
		return (TookParticular) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session
						.createCriteria(TookParticular.class);
				criteria.add(Restrictions.eq("atTookApplyPerson",
						atTookApplyPerson));
				List<TookParticular> took = criteria.list();
				if (took.size() == 0) {
					return null;
				} else {
					return took.get(0);
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public TookParticular getTookParticularByTypeId(final int tpArticleTypeId) {
		return (TookParticular) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session
						.createCriteria(TookParticular.class);
				criteria.add(Restrictions
						.eq("tpArticleTypeId", tpArticleTypeId));
				List<TookParticular> took = criteria.list();
				if (took.size() == 0) {
					return null;
				} else {
					return took.get(0);
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public TookParticular getTookParticularBydate(final String applyDate) {
		return (TookParticular) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session
						.createCriteria(TookParticular.class);
				criteria.add(Restrictions.eq("applyDate", applyDate));
				List<TookParticular> took = criteria.list();
				if (took.size() == 0) {
					return null;
				} else {
					return took.get(0);
				}
			}
		});
	}

}
